package system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import common.config.exception.CustomException;
import common.model.enums.EmailNotifyTypeEnum;
import common.model.po.SysTopic;
import common.model.po.SysTopicLike;
import common.model.po.SysUser;
import common.model.request.SendEmailRequest;
import common.model.response.base.Result;
import common.util.EmailTemplateUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import system.mapper.SysTopicLikeMapper;
import system.model.request.LikeTopicRequest;
import system.service.MailService;
import system.service.SysTopicLikeService;
import system.service.SysTopicService;
import system.service.SysUserService;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * 服务实现类
 *
 * @author 米泽鹏
 * @since 2023-09-17 下午 02:50
 */
@Service
public class SysTopicLikeServiceImpl extends ServiceImpl<SysTopicLikeMapper, SysTopicLike> implements SysTopicLikeService {

	@Resource
	private SysTopicLikeMapper mapper;
	@Resource
	private SysTopicService topicService;
	@Resource
	private MailService mailService;
	@Resource
	private SysUserService userService;
	@Value("${satisfactory.domain}")
	private String domain;

	@Override
	public Result<?> likeTopic(LikeTopicRequest request) {
		// 校验要点赞的话题是否存在
		SysTopic topic = topicService.getById(request.getTopicId());
		if (topic == null) {
			throw new CustomException("网页数据已过期，请刷新页面后重试（话题不存在）");
		}
		lambdaQuery().eq(SysTopicLike::getUserId, request.getUserId()).eq(SysTopicLike::getTopicId, request.getTopicId()).oneOpt().ifPresent(topicLike -> {
			throw new CustomException("该话题已赞过，请勿重复操作");
		});
		SysTopicLike topicLike = new SysTopicLike();
		topicLike.setUserId(request.getUserId());
		topicLike.setTopicId(request.getTopicId());
		save(topicLike);
		// 邮件通知被点赞的用户
		SysUser likeToUser = userService.getById(topic.getUserId());
		SysUser likeUser = userService.getById(topicLike.getUserId());
		if (!likeToUser.getId().equals(likeUser.getId()) && EmailNotifyTypeEnum.check(EmailNotifyTypeEnum.LIKE_ME, likeToUser)) {
			SendEmailRequest sendEmailRequest = new SendEmailRequest();
			sendEmailRequest.setToList(Collections.singletonList(likeToUser.getEmail()));
			sendEmailRequest.setSubject(EmailNotifyTypeEnum.LIKE_ME.getDesc() + "【幸福工厂游戏工具网站】");
			sendEmailRequest.setText(EmailTemplateUtil.getLikeMeContent(domain, likeUser.getNickname(), topic.getContent()));
			sendEmailRequest.setHtml(true);
			mailService.sendEmail(sendEmailRequest);
		}
		return Result.success();
	}

	@Override
	public Result<?> cancelLikeTopic(LikeTopicRequest request) {
		// 校验要取消点赞的话题是否存在
		SysTopic topic = topicService.getById(request.getTopicId());
		if (topic == null) {
			throw new CustomException("网页数据已过期，请刷新页面后重试（话题不存在）");
		}
		lambdaQuery().eq(SysTopicLike::getUserId, request.getUserId()).eq(SysTopicLike::getTopicId, request.getTopicId()).oneOpt().orElseThrow(() -> new CustomException("该话题还未赞过，无需取消"));
		lambdaUpdate().eq(SysTopicLike::getUserId, request.getUserId()).eq(SysTopicLike::getTopicId, request.getTopicId()).remove();
		return Result.success();
	}

}
